package com.zoe.onelink.logging.trace.listener;

import com.zoe.onelink.util.PathMatchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;

import java.util.Set;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-09-26
 */
@Slf4j
public class OnelinkLoggingPropertyChangeListener implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        return eventType == EnvironmentChangeEvent.class;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        EnvironmentChangeEvent envChangeEvent = (EnvironmentChangeEvent) event;
        Set<String> keys = envChangeEvent.getKeys();
        if (keys == null) {
            return;
        }
        for (String key : keys) {
            if (key.startsWith("onelink.logging")) {
                log.info("[OneLinkFrm] 清理PathMatchUtil缓存");
                PathMatchUtil.clearCache();
                return;
            }
        }
    }
}
